package org.example.service;

import lombok.extern.slf4j.Slf4j;
import org.example.entity.Contents;
import org.example.entity.ContentsIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 */

@Service
@Slf4j
public class ContentsIndexService {

    @Autowired
    private ContentsService contentsService;
    @Autowired
    private ElasticsearchTemplate template;

    /**
     * 初始化索引结构和数据
     */
    public void initIndex() {
        // 处理索引结构
        IndexOperations indexOps = template.indexOps(ContentsIndex.class);
        if (indexOps.exists()) {
            boolean delFlag = indexOps.delete();
            log.info("contents_index exists，delete:{}", delFlag);
            indexOps.createMapping(ContentsIndex.class);
        } else {
            log.info("contents_index not exists");
            indexOps.createMapping(ContentsIndex.class);
        }
        // 同步数据库表记录
        List<Contents> contentsList = contentsService.queryAll();
        if (contentsList.size() > 0) {
            List<ContentsIndex> contentsIndexList = new ArrayList<>();
            contentsList.forEach(contents -> {
                ContentsIndex contentsIndex = new ContentsIndex();
                BeanUtils.copyProperties(contents, contentsIndex);
                contentsIndexList.add(contentsIndex);
            });
            template.save(contentsIndexList);
        }
        // ID查询
        ContentsIndex contentsIndex = template.get("10", ContentsIndex.class);
        log.info("contents-index-10:{}", contentsIndex);
    }
}

